let names = []; // 存储名单
let intervalId;
let isRunning = false;

// 获取DOM元素
const fileInput = document.getElementById("fileInput");
const nameDisplay = document.getElementById("nameDisplay");
const startBtn = document.getElementById("startBtn");
const stopBtn = document.getElementById("stopBtn");

// 读取文件内容
fileInput.addEventListener("change", (event) => {
  const file = event.target.files[0];
  if (file) {
    const reader = new FileReader();
    reader.onload = (e) => {
      const content = e.target.result;
      names = content
        .split("\n")
        .map((name) => name.trim())
        .filter((name) => name); // 按行分割并去除空行
      if (names.length > 0) {
        nameDisplay.innerHTML = "<span>名单已加载，点击开始</span>";
        startBtn.disabled = false;
      } else {
        nameDisplay.innerHTML = "<span>文件内容为空，请重新上传</span>";
      }
    };
    reader.readAsText(file);
  }
});

// 随机显示名字的函数
function randomName() {
  const randomIndex = Math.floor(Math.random() * names.length);
  nameDisplay.innerHTML = `<span>${names[randomIndex]}</span>`;
}

// 开始按钮点击事件
startBtn.addEventListener("click", () => {
  if (!isRunning) {
    isRunning = true;
    intervalId = setInterval(randomName, 10); // 加快点名速度，每10毫秒更新一次名字
    startBtn.disabled = true;
    stopBtn.disabled = false;
  }
});

// 停止按钮点击事件
stopBtn.addEventListener("click", () => {
  if (isRunning) {
    clearInterval(intervalId);
    isRunning = false;
    startBtn.disabled = false;
    stopBtn.disabled = true;
  }
});
